﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OOAD_Project_Engine.Domain.Components;
using OOAD_Project_Engine.Foundation.Interfaces;
using OOAD_Project_Engine.Domain.Factories;
using OOAD_Project_Engine.Foundation.MyEventArgs;
using OOAD_Project_Editor.Presentation;
using OOAD_Project_Editor.Foundation;

namespace OOAD_Project_Editor.Presentation
{
    public partial class EntityComponentForm : Form
    {
        public List<Type> ComponentTypeTempletes { get; set; }
        Type SelectedComponentType;
        Type spriteType, transformType;
        public event EventHandler<CustomEventArgs<Type>> OnEntityComponentSelected;
        public IEntity Entity { get;private set; }

        public EntityComponentForm(IEntity entity)
        {
            InitializeComponent();
            this.Entity = entity;
            string typeStr = typeof(Sprite).AssemblyQualifiedName;
            spriteType = Type.GetType(typeStr); // TODO: Initialize to an appropriate value
            typeStr = typeof(Transform).AssemblyQualifiedName;
            transformType = Type.GetType(typeStr); // TODO: Initialize to an appropriate value

            ComponentTypeTempletes = new List<Type>();
            ComponentTypeTempletes.Add(spriteType);
            ComponentTypeTempletes.Add(transformType);
            updateList();
        }

        private void updateList()
        {
            int i = 0;
            foreach (Type t in ComponentTypeTempletes)
            {
                ListViewItem item = new ListViewItem(t.Name, i);
                item.Tag = t;
                listView1.Items.Add(item);
                i++;
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if ((int)msg.LParam == 0x203e0001)   // Alt-F4
                return true;
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.SelectedItems) //多選會是最後一個
            {
                SelectedComponentType = item.Tag as Type;
            }
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (OnEntityComponentSelected != null)
                OnEntityComponentSelected(this, new CustomEventArgs<Type>(SelectedComponentType));
            this.Close();
        }

    }
}
